package cate.common.table.d;

import cate.common.table.common.ParamSheet;
import cate.common.util.XT;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@NoteClass(value = "装备")
public class GDEquip {

    @NoteField(value = "启用新版装备附魔逻辑")
    public static boolean ENABLE_NEW_ENCHANT;

    @NoteField(value = "装备铭刻开启混沌族")
    public static boolean ENABLE_CHAOS;

    @NoteField(value = "装备开放英雄等级")
    public static int EQUIP_OPEN_LEVEL;

    private static Map<Byte, Double> REFORGE_RATES;
    private static Map<Byte, Integer> RUBBING_RATES;
    private static int TOTAL_CAMP_MARK_RATE;

    @NoteField(value = "部位1(武器)")
    public static final byte POS_1 = 1;

    @NoteField(value = "部位2(护甲)")
    public static final byte POS_2 = 2;

    @NoteField(value = "部位3(头盔)")
    public static final byte POS_3 = 3;

    @NoteField(value = "部位4(鞋子)")
    public static final byte POS_4 = 4;

    public static boolean checkPos(byte pos) {
        return pos == POS_1 || pos == POS_2 || pos == POS_3 || pos == POS_4;
    }




    //ID：10+（品质-1）+部位+职业+精英

    //示例：105460
    public static int buildEquipTid(int quality, byte pos, byte job, byte elite) {
        int tid = 100000;
        tid += (quality - 1) * 1000;
        tid += pos * 100;
        tid += job * 10;
        tid += elite;
        return tid;
    }


    public static void initialize(ParamSheet paramSheet) {
        EQUIP_OPEN_LEVEL = GDParamTool.getInt(paramSheet, 430, 60);
        ENABLE_NEW_ENCHANT = GDParamTool.getBoolean(paramSheet,3100, false);
        ENABLE_CHAOS = GDParamTool.getBoolean(paramSheet,3101, false);
        String reforgeRateStr = GDParamTool.getString(paramSheet, 200000, "0#3333#6667#10000");
        REFORGE_RATES = toReforgeRates(reforgeRateStr);
        String rubbingRateStr = GDParamTool.getString(paramSheet, 200001, "1:3000#2:3000#3:3000#4:3000#5:2000#6:2000#106:1000");
        RUBBING_RATES = toRubbingRates(rubbingRateStr);
    }

    private static Map<Byte, Double> toReforgeRates(String reforgeRateStr) {
        Map<Byte, Double> map = new HashMap<>();
        byte idx = 0;
        for (String rate : reforgeRateStr.split("#")) {
            map.put(idx++, Double.parseDouble(rate) / 10000);
        }
        return map;
    }

    private static Map<Byte, Integer> toRubbingRates(String rubbingRates) {
        Map<Byte, Integer> map = new HashMap<>();
        int tmpRate = 0;
        for (String campRate : rubbingRates.split("#")) {
            String[] arr = campRate.split(":");
            int rate = Integer.parseInt(arr[1]);
            tmpRate += rate;
            map.put(Byte.parseByte(arr[0]), tmpRate);
            TOTAL_CAMP_MARK_RATE += rate;
        }
        return map;
    }

    public static byte randomCampMark() {
        int randomIdx = XT.randomIndex(TOTAL_CAMP_MARK_RATE);
        for (byte i=1; i<RUBBING_RATES.size(); ++i) {
            if (RUBBING_RATES.getOrDefault(i, 0) > randomIdx) {
                return i;
            }
        }
        return 1;
    }

    public static boolean reforgeCampResult(int campMarkNum) {
        return Math.random() < REFORGE_RATES.getOrDefault((byte) campMarkNum, 0d);
    }
}
